package cc.mango.android.chartutil;

import static cc.mango.android.chartutil.ChartCommons.DOWN_CHART_BACKGROUND_COLOR;
import static cc.mango.android.chartutil.ChartCommons.DOWN_CHART_Y_LABEL_TEXTSIZE;
import static cc.mango.android.chartutil.ChartCommons.UP_CHART_BACKGROUND_COLOR;
import static cc.mango.android.chartutil.ChartCommons.UP_CHART_BELOW_LINE_COLOR;
import static cc.mango.android.chartutil.ChartCommons.UP_CHART_MA10_COLOR;
import static cc.mango.android.chartutil.ChartCommons.UP_CHART_MA50_COLOR;
import static cc.mango.android.chartutil.ChartCommons.UP_CHART_X_LABEL_TEXTSIZE;

import java.util.ArrayList;
import java.util.List;

import org.achartengine.chart.PointStyle;
import org.achartengine.model.XYMultipleSeriesDataset;
import org.achartengine.model.XYSeries;
import org.achartengine.renderer.XYMultipleSeriesRenderer;
import org.achartengine.renderer.XYSeriesRenderer;

import android.content.Context;
import android.graphics.Color;
import android.view.View;
import cc.mango.android.chart.CandlestickChart;
import cc.mango.android.chart.CandlestickSmaChart;
import cc.mango.android.chart.DownBarChart;
import cc.mango.android.chart.LineSmaChart;
import cc.mango.android.chart.RsiChart;
import cc.mango.android.chart.UpLineChart;
import cc.mango.android.chartview.ChartView;
import cc.mango.android.dto.StkDaily;
import cc.mango.android.dto.StkIntraday;
import cc.mango.common.FinalKey;
import cc.telecomdigital.tdstock.activity.chart.NvChart;

public class ChartViewFactory
{

	private static ChartView		aboveView;
	private static ChartView		underView;

	private static List<Integer>	xLabelIndexs	= new ArrayList<Integer>();

	static double[]			yBoundary		= new double[2];

	public static View getStkLineChartView(Context context, List list,
			String dateType)
	{

		if (list == null || list.size() == 0)
		{

			return null;

		}

		aboveView = new ChartView(context);

		XYMultipleSeriesDataset dataset = getCloseDataset(list);

		XYMultipleSeriesRenderer mRenderer = getUpRenderer();

		//		double[] minAndMax = getIntradayMaxAndMinYLabel(list);

		//设置y轴的最大值和最小值
		mRenderer.setYAxisMin(yBoundary[0]);
		mRenderer.setYAxisMax(yBoundary[1]);

		UpLineChart chart = new UpLineChart(dataset, mRenderer);

		aboveView.setChart(chart);

		chart.setNvChart((NvChart) context);

		if (FinalKey.INTRADAY_DATETYPE_TODAY.equals(dateType))
		{
			chart.setXLabels(getOneDayXLabels(list, 6));
		}
		else if (FinalKey.INTRADAY_DATETYPE_WEEK.equals(dateType))
		{

			List<String> xLabels = getWeekXLabels(list, 5);

			chart.setXLabels(xLabels);

			chart.setXLabelIndexs(xLabelIndexs);

		}
		else if (FinalKey.DAILY_DATETYPE_MONTH.equals(dateType)
				|| FinalKey.DAILY_DATETYPE_3MONTHS.equals(dateType)
				|| FinalKey.DAILY_DATETYPE_6MONTHS.equals(dateType))
		{
			chart.setXLabels(getMonthXLabels(list, 6));
		}
		else if (FinalKey.DAILY_DATETYPE_YEAR.equals(dateType)
				|| FinalKey.DAILY_DATETYPE_2YEARS.equals(dateType))
		{
			chart.setXLabels(getYearXLabels(list, 6));
		}

		return aboveView;

	}

	public static View getStkVolChartView(Context context, List list)
	{

		if (list == null || list.size() == 0)
		{

			return null;

		}

		underView = new ChartView(context);

		//		AbstractChart chart = new DownBarChart(getVolDataset(list),getDownRenderer());
		//		
		DownBarChart chart = new DownBarChart(getVolDataset(list),
				getDownRenderer(), DownBarChart.Type.LINE);

		chart.setNvChart((NvChart) context);

		underView.setChart(chart);

		return underView;

	}

	//Rsi chartView
	public static View getStkRsiChartView(Context context, List list)
	{

		if (list == null || list.size() == 0)
		{

			return null;

		}

		underView = new ChartView(context);

		RsiChart chart = new RsiChart(getRsiDataset(list), getRsiRenderer());

		chart.setNvChart((NvChart) context);

		underView.setChart(chart);

		return underView;

	}

	private static XYMultipleSeriesRenderer getUpRenderer()
	{

		XYMultipleSeriesRenderer renderer = getCommonUpRenderer();

		XYSeriesRenderer b = new XYSeriesRenderer();
		b.setPointStyle(PointStyle.POINT);
		b.setColor(Color.WHITE);
		b.setFillPoints(true);
		b.setFillBelowLineColor(UP_CHART_BELOW_LINE_COLOR);//last
		b.setFillBelowLine(true);

		renderer.addSeriesRenderer(b);

		return renderer;

	}

	private static XYMultipleSeriesDataset getCloseDataset(List list)
	{

		XYMultipleSeriesDataset dataset = new XYMultipleSeriesDataset();

		XYSeries series = new XYSeries("close  series  dataset");

		Object data = list.get(0);

		yBoundary[0] = 1000000000;//min
		yBoundary[1] = -1;//max

		double high = -1;
		double low = 1000000000;

		if (data instanceof StkIntraday)
		{

			for (int i = 0; i < list.size(); i++)
			{

				StkIntraday temp = (StkIntraday) list.get(i);
				series.add(i, temp.getClose());

				high = temp.getDayhigh();
				low = temp.getDaylow();

				if (high > yBoundary[1])
				{

					yBoundary[1] = high;

				}

				if (low < yBoundary[0])
				{

					yBoundary[0] = low;
				}

			}

		}
		else if (data instanceof StkDaily)
		{

			for (int i = 0; i < list.size(); i++)
			{

				StkDaily temp = (StkDaily) list.get(i);

				series.add(i, temp.getClose());

				high = temp.getHigh();
				low = temp.getLow();

				if (high > yBoundary[1])
				{

					yBoundary[1] = high;

				}

				if (low < yBoundary[0])
				{

					yBoundary[0] = low;
				}

			}
		}

		dataset.addSeries(series);

		return dataset;
	}

	private static XYMultipleSeriesDataset getLineSmaDataset(
			List<StkDaily> list)
	{

		XYMultipleSeriesDataset dataset = new XYMultipleSeriesDataset();

		XYSeries series = new XYSeries("close  series  dataset");
		XYSeries seriesMa10 = new XYSeries("close  series  dataset");
		XYSeries seriesMa50 = new XYSeries("close  series  dataset");

		yBoundary[0] = 1000000000;//min
		yBoundary[1] = -1;//max

		double high = -1;
		double low = 1000000000;

		for (int i = 0; i < list.size(); i++)
		{

			StkDaily temp = (StkDaily) list.get(i);

			series.add(i, temp.getClose());
			seriesMa10.add(i, temp.getSma10());
			seriesMa50.add(i, temp.getSma50());

			high = temp.getHigh();
			low = temp.getLow();

			if (high > yBoundary[1])
			{

				yBoundary[1] = high;

			}

			if (low < yBoundary[0])
			{

				yBoundary[0] = low;
			}

		}

		dataset.addSeries(series);
		dataset.addSeries(seriesMa10);
		dataset.addSeries(seriesMa50);

		return dataset;
	}

	////////////////////////////////////////////////////////////////////////

	private static XYMultipleSeriesDataset getVolDataset(List list)
	{

		XYMultipleSeriesDataset dataset = new XYMultipleSeriesDataset();

		XYSeries series = new XYSeries("vol series dataset");

		Object data = list.get(0);

		if (data instanceof StkIntraday)
		{

			for (int i = 0; i < list.size(); i++)
			{

				series.add(i, ((StkIntraday) list.get(i)).getVol());

			}

		}
		else if (data instanceof StkDaily)
		{

			for (int i = 0; i < list.size(); i++)
			{

				series.add(i, ((StkDaily) list.get(i)).getVol());

			}
		}

		dataset.addSeries(series);

		return dataset;
	}

	private static XYMultipleSeriesRenderer getDownRenderer()
	{
		XYMultipleSeriesRenderer renderer = getCommonDownRenderer();

		XYSeriesRenderer b = new XYSeriesRenderer();
		b.setPointStyle(PointStyle.POINT);
		b.setColor(Color.WHITE);
		b.setFillPoints(true);

		renderer.addSeriesRenderer(b);

		return renderer;
	}

	public static List<String> getOneDayXLabels(List<StkIntraday> list,
			int numLabels)
	{

		int size = list.size();

		List<String> xLabels = new ArrayList<String>();

		String label = "";

		String time;

		/*if(size <= numLabels){
		
			for(int i = 0; i < size; i++){   	  				
				time = ((StkIntraday)list.get(i)).getTime(); 
			label = time.substring(0, 2) + ":" + time.substring(2, 4);					
			xLabels.add(label);
			}
			
		}else{
		
		for(int i = 0; i < numLabels-1; i++){
				int index = Math.round(1.0f * i * size / (numLabels - 1));
				
				time = ((StkIntraday)list.get(index)).getTime(); 
			label = time.substring(0, 2) + ":" + time.substring(2, 4);					
			xLabels.add(label);
			}
		
		time = ((StkIntraday)list.get(size - 1)).getTime(); 
		label = time.substring(0, 2) + ":" + time.substring(2, 4);					
		xLabels.add(label);
		
		}*/

		if (size < 20)
		{
			//第一个点
			time = (list.get(0)).getTime();
			label = time.substring(0, 2) + ":" + time.substring(2, 4);
			xLabels.add(label);

		}
		else if (size < 65)
		{//大于20，小于65个点时的情况
			//第一个点
			time = (list.get(0)).getTime();
			label = time.substring(0, 2) + ":" + time.substring(2, 4);
			xLabels.add(label);
			//最后一个点
			//				int index = getIndexFromDecimal((size - 1) * 0.6f);
			int index = getOneDayIndexFromDecimal((size - 1) * 0.6f);

			time = (list.get(index)).getTime();
			label = time.substring(0, 2) + ":" + time.substring(2, 4);
			xLabels.add(label);

		}
		else
		{

			for (int i = 0; i < numLabels - 1; i++)
			{
				//   	  				int index = Math.round(1.0f * i * size / (numLabels - 1));
				int index = getOneDayIndexFromDecimal(1.0f * i * size
						/ (numLabels - 1));

				time = (list.get(index)).getTime();
				label = time.substring(0, 2) + ":" + time.substring(2, 4);
				xLabels.add(label);
			}

			time = (list.get(size - 1)).getTime();
			label = time.substring(0, 2) + ":" + time.substring(2, 4);
			xLabels.add(label);
		}

		return xLabels;
	}

	/**
	 * get 1day,1month,3month,6month xLabels
	 */
	public static List<String> getMonthXLabels(List<StkDaily> list,
			int numLabels)
	{

		int size = list.size();

		List<String> xLabels = new ArrayList<String>();

		String label = "";

		String time;

		String lastMonth;

		String month;
		String day;

		//第一个X坐标
		time = (list.get(0)).getDtyyyymmdd();
		month = time.substring(4, 6);
		day = time.substring(6, 8);
		label = month + "月" + day;

		xLabels.add(label);

		lastMonth = month;

		//第一个和最后一个之间的X坐标
		for (int i = 1; i < numLabels - 1; i++)
		{
			//  				int index = Math.round(1.0f * i * size / (numLabels - 1));//原
			int index = getIndexFromDecimal(1.0f * i * size
					/ (numLabels - 1));//iPhone

			time = (list.get(index)).getDtyyyymmdd();
			month = time.substring(4, 6);
			day = time.substring(6, 8);
			if (month.equals(lastMonth))
			{
				label = day;
			}
			else
			{
				label = month + "月" + day;
				lastMonth = month;
			}

			xLabels.add(label);
		}

		//最后一个X坐标
		time = (list.get(size - 1)).getDtyyyymmdd();
		month = time.substring(4, 6);
		day = time.substring(6, 8);
		if (month.equals(lastMonth))
		{
			label = day;
		}
		else
		{
			label = month + "月" + day;
			lastMonth = month;
		}

		xLabels.add(label);

		return xLabels;
	}

	/**
	 * get 1week xLabels
	 */
	/*
	public static List<String> getWeekXLabels(List<StkIntraday> list, int numLabels){
	 
	 	Object data  = list.get(0); 
	 	
	 	int size = list.size();

	 	List<String> xLabels = new ArrayList<String>();
	 	
	 	String label = "";
	 	
	 	String time;
	 	
	 	String lastMonth ;
	 	
	 	String month;
	 	String day;
	 	
	 	int lastIndex = 0;
	 
		if(data instanceof StkIntraday) {	  			
				//第一个X坐标
	 			time = ((StkIntraday)list.get(0)).getDtyyyymmdd(); 
	  			month = time.substring(4, 6);
	  			day = time.substring(6,8);
	  			label = month+"月" + day;
	  				
				xLabels.add(label);		  			
			
				lastMonth = month;
				
				xLabelIndexs.clear();//清空list
				
				xLabelIndexs.add(0);
				
				lastIndex = lastIndex +1;
				
				//第一个和最后一个之间的X坐标
				for(int i = 1; i < numLabels-1; i++){
	  				int index = Math.round(1.0f * i * size / (numLabels - 1));
	   	  			time = ((StkIntraday)list.get(index)).getDtyyyymmdd(); 
	   	  			month = time.substring(4, 6);
	   	  			day = time.substring(6,8);
	   	  			if(month.equals(lastMonth)){
	   	  				label = day;
	   	  			}else{
	   	  				label = month+"月" + day;
	   	  				lastMonth = month;
	   	  			}
							
					xLabels.add(label);
					
					lastIndex = findIndexByDate(list,time,lastIndex);
					
					lastIndex = lastIndex +1;
	  			}
				
				
				//最后一个X坐标
				time = ((StkIntraday)list.get(size - 1)).getDtyyyymmdd(); 
	  			month = time.substring(4, 6);
	  			day = time.substring(6,8);
	  			if(month.equals(lastMonth)){
	  				label = day;
	  			}else{
	  				label = month+"月" + day;
	  				lastMonth = month;
	  			}
					
	  			
	  			if(!label.equals(xLabels.get(xLabels.size() - 1))){
	  				
	  				xLabels.add(label);
				
	  				lastIndex = findIndexByDate(list,time,lastIndex);
	  			}
	  			
				
			
		}
		
		return xLabels;
	}*/

	public static List<String> getWeekXLabels(List<StkIntraday> list,
			int numLabels)
	{

		Object data = list.get(0);

		int size = list.size();

		List<String> xLabels = new ArrayList<String>();

		String label = "";

		String time;

		String lastMonth;

		String month;
		String day;

		String lastTime;

		//		 	int lastIndex = 0;

		//第一个X坐标
		time = (list.get(0)).getDtyyyymmdd();
		month = time.substring(4, 6);
		day = time.substring(6, 8);
		label = month + "月" + day;

		xLabels.add(label);

		lastMonth = month;

		xLabelIndexs.clear();//清空list

		xLabelIndexs.add(0);

		//	  				lastIndex = lastIndex +1;
		lastTime = time;

		//其他X坐标
		for (int i = 0; i < size; i++)
		{

			time = (list.get(i)).getDtyyyymmdd();

			if (lastTime.equals(time))
			{//如果于上一个lable相等，则进入下一次

				continue;
			}

			xLabelIndexs.add(i);

			month = time.substring(4, 6);
			day = time.substring(6, 8);
			if (month.equals(lastMonth))
			{
				label = day;
			}
			else
			{
				label = month + "月" + day;
				lastMonth = month;
			}

			xLabels.add(label);

			lastTime = time;

		}

		return xLabels;
	}

	/**
	 * get 1year,2years xLabels
	 */
	public static List<String> getYearXLabels(List<StkDaily> list,
			int numLabels)
	{

		int size = list.size();

		/* 	if(size < numLabels){
				
		 		numLabels = size;
			}*/

		List<String> xLabels = new ArrayList<String>();

		String label = "";

		String time;

		String month;

		String year;

		String lastYear;

		//最第一个X坐标
		time = (list.get(0)).getDtyyyymmdd();
		month = time.substring(4, 6);
		year = time.substring(2, 4);
		label = year + "年" + month;

		lastYear = year;

		xLabels.add(label);

		//第一个和最后一个之间的X坐标
		for (int i = 1; i < numLabels - 1; i++)
		{
			//  				int index = Math.round(1.0f * i * size / (numLabels - 1));//原

			int index = getIndexFromDecimal(1.0f * i * size
					/ (numLabels - 1));//iPhone
			time = (list.get(index)).getDtyyyymmdd();
			month = time.substring(4, 6);
			year = time.substring(2, 4);
			if (year.equals(lastYear))
			{
				label = month;
			}
			else
			{
				label = year + "年" + month;
				lastYear = year;
			}

			xLabels.add(label);
		}

		//最后一个X坐标
		time = (list.get(size - 1)).getDtyyyymmdd();
		month = time.substring(4, 6);
		year = time.substring(2, 4);
		if (year.equals(lastYear))
		{
			label = month;
		}
		else
		{
			label = year + "年" + month;
			lastYear = year;
		}

		xLabels.add(label);

		return xLabels;
	}

	//Ris chart renderer
	private static XYMultipleSeriesRenderer getRsiRenderer()
	{

		XYMultipleSeriesRenderer renderer = getCommonDownRenderer();

		XYSeriesRenderer b = new XYSeriesRenderer();
		b.setPointStyle(PointStyle.POINT);
		b.setColor(Color.WHITE);
		b.setFillPoints(true);

		b.setFillBelowLine(false);//线下没有颜色

		renderer.addSeriesRenderer(b);

		//设置y轴的最大值和最小值
		renderer.setYAxisMin(0);
		renderer.setYAxisMax(100);

		return renderer;

	}

	private static XYMultipleSeriesDataset getRsiDataset(List list)
	{

		XYMultipleSeriesDataset dataset = new XYMultipleSeriesDataset();

		XYSeries series = new XYSeries("Rsi  series  dataset");

		Object data = list.get(0);

		if (data instanceof StkDaily)
		{

			for (int i = 0; i < list.size(); i++)
			{

				series.add(i, ((StkDaily) list.get(i)).getRsi());

			}
		}

		dataset.addSeries(series);

		return dataset;
	}

	/* 
	 private static int findIndexByDate(List<StkIntraday> list,String date,int startIndex){
		 
		 
		 for(int i=startIndex;i<list.size();i++){
			 
			 if(list.get(i).getDtyyyymmdd().equals(date)){				 
				 
				 xLabelIndexs.add(i);
				 
				 TDStockLog.d("Print_Info", "index:"+i);
				 
				 return i;
				 
			 }
			 
		 }
		 
		 return -1;
	 }
	 
	 
	 private static List<Integer> getWeekXLabelIndexs(List<StkIntraday> list,List<String> dates){
		 
		 List<Integer> xLabelIndexs = new ArrayList<Integer>();
		 
		 
		 int index = 0;
		 
		 xLabelIndexs.add(index);
		 
		 index = index + 1;
		 
		 TDStockLog.d("Print_Info", "Data.size:"+dates.size());
		 
		 for(int i=1;i<dates.size();i++){
			 
			 index = findIndexByDate(list,dates.get(i),0);
			 
			 if(index !=-1){
				 xLabelIndexs.add(index);
				 index = index + 1;
			 }
			 
			 TDStockLog.d("Print_Info", "cvf  :  index:"+index+"  Date:"+dates.get(i));
			 			 
		 }		 
		 
		 return xLabelIndexs;
	 }
	 
	 //y坐标的最大和最小值, 也是图表的上限和下限
	 private static double[] getIntradayMaxAndMinYLabel(List<StkIntraday> list){
		 
		 double[] yBoundary = new double[2]; 
		 yBoundary[0] = 1000000000;

		 double high = -1;
		 double low = 0;
		 
		 for(StkIntraday data : list){
			 
			 high = data.getDayhigh();
			 low = data.getDaylow();
			 
			 if(high > yBoundary[1]){
				 
				 yBoundary[1] = high;
				 
			 }
			 
			 if(low < yBoundary[0]){
				 
				 yBoundary[0] = low;
			 }
			 
		 }

		 
		 return yBoundary;
	 }
	 
	 
	 */

	public static View getStkLineSmaChartView(Context context, List list,
			String dateType)
	{

		if (list == null || list.size() == 0)
		{

			return null;

		}

		aboveView = new ChartView(context);

		XYMultipleSeriesDataset dataset = getLineSmaDataset(list);

		XYMultipleSeriesRenderer mRenderer = getLineSmaRenderer();

		//					double[] minAndMax = getIntradayMaxAndMinYLabel(list);

		//设置y轴的最大值和最小值
		mRenderer.setYAxisMin(yBoundary[0]);
		mRenderer.setYAxisMax(yBoundary[1]);

		LineSmaChart chart = new LineSmaChart(dataset, mRenderer);

		aboveView.setChart(chart);

		chart.setNvChart((NvChart) context);

		if (FinalKey.INTRADAY_DATETYPE_TODAY.equals(dateType))
		{
			chart.setXLabels(getOneDayXLabels(list, 6));
		}
		else if (FinalKey.INTRADAY_DATETYPE_WEEK.equals(dateType))
		{

			List<String> xLabels = getWeekXLabels(list, 5);

			chart.setXLabels(xLabels);

			chart.setXLabelIndexs(xLabelIndexs);

		}
		else if (FinalKey.DAILY_DATETYPE_MONTH.equals(dateType)
				|| FinalKey.DAILY_DATETYPE_3MONTHS.equals(dateType)
				|| FinalKey.DAILY_DATETYPE_6MONTHS.equals(dateType))
		{
			chart.setXLabels(getMonthXLabels(list, 6));
		}
		else if (FinalKey.DAILY_DATETYPE_YEAR.equals(dateType)
				|| FinalKey.DAILY_DATETYPE_2YEARS.equals(dateType))
		{
			chart.setXLabels(getYearXLabels(list, 6));
		}

		return aboveView;

	}

	private static XYMultipleSeriesRenderer getLineSmaRenderer()
	{

		XYMultipleSeriesRenderer renderer = getCommonUpRenderer();

		XYSeriesRenderer b = new XYSeriesRenderer();
		b.setPointStyle(PointStyle.POINT);
		b.setColor(Color.WHITE);
		b.setFillPoints(true);
		b.setFillBelowLineColor(UP_CHART_BELOW_LINE_COLOR);//last
		b.setFillBelowLine(true);

		renderer.addSeriesRenderer(b);

		XYSeriesRenderer rma10 = new XYSeriesRenderer();
		rma10.setPointStyle(PointStyle.POINT);
		rma10.setColor(UP_CHART_MA10_COLOR);
		rma10.setFillPoints(true);
		rma10.setFillBelowLine(false);

		renderer.addSeriesRenderer(rma10);

		XYSeriesRenderer rma50 = new XYSeriesRenderer();
		rma50.setPointStyle(PointStyle.POINT);
		rma50.setColor(UP_CHART_MA50_COLOR);
		rma50.setFillPoints(true);
		rma50.setFillBelowLine(false);

		renderer.addSeriesRenderer(rma50);

		return renderer;

	}

	public static View getCandlestickChartView(Context context, List list,
			String dateType)
	{

		if (list == null || list.size() == 0)
		{

			return null;

		}

		aboveView = new ChartView(context);

		XYMultipleSeriesDataset dataset = getCloseDataset(list);

		XYMultipleSeriesRenderer mRenderer = getUpRenderer();

		//设置y轴的最大值和最小值
		mRenderer.setYAxisMin(yBoundary[0]);
		mRenderer.setYAxisMax(yBoundary[1]);

		CandlestickChart chart = new CandlestickChart(dataset, mRenderer,
				(NvChart) context, list);

		aboveView.setChart(chart);

		//				chart.setNvChart((NvChart)context); 

		if (FinalKey.INTRADAY_DATETYPE_TODAY.equals(dateType))
		{
			chart.setXLabels(getOneDayXLabels(list, 6));
		}
		else if (FinalKey.INTRADAY_DATETYPE_WEEK.equals(dateType))
		{

			List<String> xLabels = getWeekXLabels(list, 5);

			chart.setXLabels(xLabels);

			chart.setXLabelIndexs(xLabelIndexs);

		}
		else if (FinalKey.DAILY_DATETYPE_MONTH.equals(dateType)
				|| FinalKey.DAILY_DATETYPE_3MONTHS.equals(dateType)
				|| FinalKey.DAILY_DATETYPE_6MONTHS.equals(dateType))
		{
			chart.setXLabels(getMonthXLabels(list, 6));
		}
		else if (FinalKey.DAILY_DATETYPE_YEAR.equals(dateType)
				|| FinalKey.DAILY_DATETYPE_2YEARS.equals(dateType))
		{
			chart.setXLabels(getYearXLabels(list, 6));
		}

		return aboveView;

	}

	public static View getCandlestickSmaChartView(Context context, List list,
			String dateType)
	{

		if (list == null || list.size() == 0)
		{

			return null;

		}

		aboveView = new ChartView(context);

		XYMultipleSeriesDataset dataset = getLineSmaDataset(list);

		XYMultipleSeriesRenderer mRenderer = getLineSmaRenderer();

		//设置y轴的最大值和最小值
		mRenderer.setYAxisMin(yBoundary[0]);
		mRenderer.setYAxisMax(yBoundary[1]);

		CandlestickSmaChart chart = new CandlestickSmaChart(dataset,
				mRenderer, (NvChart) context, list);

		aboveView.setChart(chart);

		//			chart.setNvChart((NvChart)context); 

		if (FinalKey.INTRADAY_DATETYPE_TODAY.equals(dateType))
		{
			chart.setXLabels(getOneDayXLabels(list, 6));
		}
		else if (FinalKey.INTRADAY_DATETYPE_WEEK.equals(dateType))
		{

			List<String> xLabels = getWeekXLabels(list, 5);

			chart.setXLabels(xLabels);

			chart.setXLabelIndexs(xLabelIndexs);

		}
		else if (FinalKey.DAILY_DATETYPE_MONTH.equals(dateType)
				|| FinalKey.DAILY_DATETYPE_3MONTHS.equals(dateType)
				|| FinalKey.DAILY_DATETYPE_6MONTHS.equals(dateType))
		{
			chart.setXLabels(getMonthXLabels(list, 6));
		}
		else if (FinalKey.DAILY_DATETYPE_YEAR.equals(dateType)
				|| FinalKey.DAILY_DATETYPE_2YEARS.equals(dateType))
		{
			chart.setXLabels(getYearXLabels(list, 6));
		}

		return aboveView;

	}

	private static XYMultipleSeriesRenderer getCommonUpRenderer()
	{

		XYMultipleSeriesRenderer renderer = new XYMultipleSeriesRenderer();
		//x,y label
		renderer.setShowXlabels(true);
		renderer.setXLabels(6);
		renderer.setShowYlabels(true);
		renderer.setYLabels(4);

		renderer.setLabelsTextSize(UP_CHART_X_LABEL_TEXTSIZE); //label  textSize

		renderer.setShowAxes(false);

		renderer.setLabelsColor(Color.LTGRAY);
		renderer.setBackgroundColor(UP_CHART_BACKGROUND_COLOR);//last

		renderer.setApplyBackgroundColor(true);

		//			legend
		renderer.setShowLegend(false);
		//		    renderer.setLegendSize(0);

		return renderer;
	}

	private static XYMultipleSeriesRenderer getCommonDownRenderer()
	{

		XYMultipleSeriesRenderer renderer = new XYMultipleSeriesRenderer();
		//x,y label
		renderer.setShowXlabels(true);
		renderer.setShowXlabels(false);
		renderer.setXLabels(6);
		renderer.setShowYlabels(true);
		renderer.setYLabels(3);

		renderer.setLabelsTextSize(DOWN_CHART_Y_LABEL_TEXTSIZE); //label  textSize

		renderer.setShowAxes(false);

		renderer.setLabelsColor(Color.LTGRAY);
		renderer.setBackgroundColor(DOWN_CHART_BACKGROUND_COLOR);//last
		renderer.setApplyBackgroundColor(true);

		renderer.setShowLegend(false);
		renderer.setLegendSize(0);

		return renderer;
	}

	private static XYMultipleSeriesDataset getForexDataset(List list)
	{

		XYMultipleSeriesDataset dataset = new XYMultipleSeriesDataset();

		XYSeries series = new XYSeries("forex  series  dataset");

		Object data = list.get(0);

		double high = -1;
		double low = 1000000000;

		if (data instanceof StkIntraday)
		{

			for (int i = 0; i < list.size(); i++)
			{

				StkIntraday temp = (StkIntraday) list.get(i);
				series.add(i, temp.getClose());

			}

		}
		else if (data instanceof StkDaily)
		{

			for (int i = 0; i < list.size(); i++)
			{

				StkDaily temp = (StkDaily) list.get(i);

				series.add(i, temp.getClose());

			}
		}

		dataset.addSeries(series);

		return dataset;
	}

	public static View getForexLineChartView(Context context, List list,
			String dateType)
	{

		if (list == null || list.size() == 0)
		{

			return null;

		}

		aboveView = new ChartView(context);

		XYMultipleSeriesDataset dataset = getCloseDataset(list);

		XYMultipleSeriesRenderer mRenderer = getUpRenderer();

		//			double[] minAndMax = getIntradayMaxAndMinYLabel(list);

		//设置y轴的最大值和最小值
		mRenderer.setYAxisMin(yBoundary[0]);
		mRenderer.setYAxisMax(yBoundary[1]);

		UpLineChart chart = new UpLineChart(dataset, mRenderer);

		aboveView.setChart(chart);

		chart.setNvChart((NvChart) context);

		if (FinalKey.INTRADAY_DATETYPE_TODAY.equals(dateType))
		{
			chart.setXLabels(getOneDayXLabels(list, 6));
		}
		else if (FinalKey.INTRADAY_DATETYPE_WEEK.equals(dateType))
		{

			/*List<String> xLabels = getWeekXLabels(list, 5);
			
			chart.setXLabels(xLabels);
			
			chart.setXLabelIndexs(xLabelIndexs);*/

			chart.setXLabels(getForexWeekXLabels(list, 6));

		}
		else if (FinalKey.DAILY_DATETYPE_MONTH.equals(dateType)
				|| FinalKey.DAILY_DATETYPE_3MONTHS.equals(dateType)
				|| FinalKey.DAILY_DATETYPE_6MONTHS.equals(dateType))
		{
			chart.setXLabels(getMonthXLabels(list, 6));
		}
		else if (FinalKey.DAILY_DATETYPE_YEAR.equals(dateType)
				|| FinalKey.DAILY_DATETYPE_2YEARS.equals(dateType))
		{
			chart.setXLabels(getYearXLabels(list, 6));
		}

		return aboveView;

	}

	private static int getIndexFromDecimal(float d)
	{

		int index = (int) d;

		//	 		int index = Math.round(d);
		//	 		
		//	 		int index = (int)Math.ceil(d);
		//	 		
		return index;
	}

	private static int getOneDayIndexFromDecimal(float d)
	{

		//	 		int index = (int)d;

		//	 		int index = Math.round(d);

		int index = (int) Math.ceil(d);

		return index;
	}

	/**
	* get 1day,1month,3month,6month xLabels
	*/
	private static List<String> getForexWeekXLabels(List<StkIntraday> list,
			int numLabels)
	{

		int size = list.size();

		List<String> xLabels = new ArrayList<String>();

		String label = "";

		String time;

		String lastMonth;

		String month;
		String day;

		//第一个X坐标
		time = (list.get(0)).getDtyyyymmdd();
		month = time.substring(4, 6);
		day = time.substring(6, 8);
		label = month + "月" + day;

		xLabels.add(label);

		lastMonth = month;

		//第一个和最后一个之间的X坐标
		for (int i = 1; i < numLabels - 1; i++)
		{
			//	  				int index = Math.round(1.0f * i * size / (numLabels - 1));//原
			int index = getIndexFromDecimal(1.0f * i * size
					/ (numLabels - 1));//iPhone

			time = (list.get(index)).getDtyyyymmdd();
			month = time.substring(4, 6);
			day = time.substring(6, 8);
			if (month.equals(lastMonth))
			{
				label = day;
			}
			else
			{
				label = month + "月" + day;
				lastMonth = month;
			}

			xLabels.add(label);
		}

		//最后一个X坐标
		time = (list.get(size - 1)).getDtyyyymmdd();
		month = time.substring(4, 6);
		day = time.substring(6, 8);
		if (month.equals(lastMonth))
		{
			label = day;
		}
		else
		{
			label = month + "月" + day;
			lastMonth = month;
		}

		xLabels.add(label);

		return xLabels;
	}

}